package com.example.administrator.zyfypt111pzy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.zyfypt111pzy.iface.LoginListener;
import com.example.administrator.zyfypt111pzy.R;
import com.example.administrator.zyfypt111pzy.bean.LoginBean;
import com.example.administrator.zyfypt111pzy.bean.SessionIDBean;
import com.example.administrator.zyfypt111pzy.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginListener {
    private EditText user,password;//用户名密码
    private Button login;//登录注册按钮
    private LoginModel loginModel;
    private SessionIDBean sessionIDBean;
    private TextView tv_btn;
    private Switch aSwitch;
    private String username="",pass="";
    private SharedPreferences sp;//简单存储对象

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        toolBarInit();//初始化标头
        //获取sp对象
        sp=getSharedPreferences("Save",MODE_PRIVATE);
        //获取
        readSP();
    }
    private void toolBarInit() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_login);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("登录界面");
    }
    private void init(){
        user=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        login=(Button)findViewById(R.id.button_login);
        tv_btn=(TextView)findViewById(R.id.textView_register_btn) ;
        aSwitch=(Switch)findViewById(R.id.switch1);
        //注册监听器
        login.setOnClickListener(this);
        tv_btn.setOnClickListener(this);
    }
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",pass);
        editor.putBoolean("remember",aSwitch.isChecked());
        editor.commit();
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            user.setText(name);
            password.setText(pass);
            aSwitch.setChecked(b);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_login:
                loginModel=new LoginModel();
                loginModel.getResultList(user.getText().toString(),password.getText().toString(),this);
                username=user.getText().toString();
                pass=password.getText().toString();
                break;
            case R.id.textView_register_btn:
                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void onResponse(LoginBean beanList) {
        sessionIDBean=new SessionIDBean();
        sessionIDBean.getSessionIDBean=beanList.getSessionid();
        String realName=sessionIDBean.realName=beanList.getRealname();
        if(!"".equals(beanList.getError())){
            Toast.makeText(this,"请输入正确的用户名和密码", Toast.LENGTH_SHORT).show();
        }else if(user.length()==0||  password.length()==0){
            Toast.makeText(this,"请输入用户名和密码", Toast.LENGTH_SHORT).show();
        }else{
            saveSP();
            Toast.makeText(this,"欢迎"+realName+"使用本软件", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(LoginActivity.this,Homepage.class);
            startActivity(intent);
        }
    }
    @Override
    public void onFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
